package com.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.client.VodClient;
import com.edu.entity.Video;
import com.edu.mapper.VideoMapper;
import com.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-17
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

	@Autowired
	private VodClient vodClient;

	//1 根据课程id删除小节
	// 删除小节，删除对应视频文件(已完成）
	@Override
	public void removeVideoByCourseId(String courseId) {
		//1 根据课程id查询课程所有的视频id
		QueryWrapper<Video> wrapperVideo = new QueryWrapper<>();
		wrapperVideo.eq("course_id",courseId);
		wrapperVideo.select("video_source_id");
		List<Video> videoList = baseMapper.selectList(wrapperVideo);

		List<String> videoIds = new ArrayList<>();
		for (int i =0; i<videoList.size();i++){
			Video video = videoList.get(i);
			String videoSourceId = video.getVideoSourceId();
			if (StringUtils.hasText(videoSourceId)){
				videoIds.add(videoSourceId);
			}
		}

		if (videoIds.size()>0){
			vodClient.deleteBatch(videoIds);
		}

		QueryWrapper<Video> wrapper = new QueryWrapper<>();
		wrapper.eq("course_id",courseId);
		baseMapper.delete(wrapper);

	}
}
